package grsu.epam.airline.action;

import java.util.HashMap;
import java.util.Map;

import grsu.epam.airline.form.EmployeeModifyForm;
import grsu.epam.airline.service.EmployeeService;
import grsu.epam.airline.service.SpecialityService;
import grsu.epam.airline.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.LookupDispatchAction;

public class EmployeesAction extends LookupDispatchAction {
	String target = null;

	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.DISPATCHER)) {
			SpecialityService specialityService = new SpecialityService();
			request.setAttribute("specialities",
					specialityService.getAllSpeciality());
			target = ForwardActionTo.ADDEMPLOYEE;
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.DISPATCHER)) {
			EmployeeModifyForm input = (EmployeeModifyForm) form;
			EmployeeService employeeService = new EmployeeService();
			if (input.getEmployeeId() != 0) {
				employeeService.deleteEmployee(input.getEmployeeId());
				target = ForwardActionTo.DELETEEMPLOYEE;
			} else {
				ActionMessages errors = new ActionMessages();
				errors.add("chooseEmployee",
						new ActionMessage("error.employee"));
				target = ForwardActionTo.ERROR;
				saveErrors(request, errors);
			}
			request.setAttribute("employees", employeeService.getAllEmployee());
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);
	}

	public ActionForward update(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		HttpSession session = request.getSession();
		UserService userService = new UserService();
		if (userService.checkUser(session.getAttribute("auth"),
				ConstantUserRole.DISPATCHER)) {
			EmployeeModifyForm input = (EmployeeModifyForm) form;
			EmployeeService employeeService = new EmployeeService();
			if (input.getEmployeeId() != 0) {
				request.setAttribute("employee",
						employeeService.getEmployeeById(input.getEmployeeId()));
				SpecialityService specialityService = new SpecialityService();
				request.setAttribute("specialities",
						specialityService.getAllSpeciality());
				target = ForwardActionTo.UPDATEEMPLOYEE;
			} else {
				ActionMessages errors = new ActionMessages();
				errors.add("chooseEmployee",
						new ActionMessage("error.employee"));
				target = ForwardActionTo.ERROR;
				saveErrors(request, errors);
				request.setAttribute("employees",
						employeeService.getAllEmployee());
			}
		} else {
			target = ForwardActionTo.ERRORUSER;
		}
		return mapping.findForward(target);
	}

	@Override
	protected Map getKeyMethodMap() {
		Map m = new HashMap();
		m.put("employee.delete", "delete");
		m.put("employee.update", "update");
		m.put("employee.add", "add");
		return m;
	}
}